<template>
  <div class="newsong">
    <van-nav-bar left-text="新歌速递" left-arrow @click-left="onClickLeft" />

    <!-- 歌曲列表 -->
    <div
      class="tabbox"
      v-for="(item, index) in newsongData"
      :key="index"
      @click="
        playAudio(item.id, item.album.picUrl, item.name, item.artists[0].name)
      "
    >
      <!-- 封图 -->
      <div class="sealing">
        <van-image :src="item.album.picUrl" lazy-load>
          <template v-slot:loading>
            <van-loading type="spinner" size="20" />
          </template>
        </van-image>
      </div>

      <!-- 名字 -->
      <div class="tabname">
        <div class="tabsongname">{{ item.name }}</div>
        <div class="tabtxt">
          <div class="textbox">SQ</div>
          {{ item.artists[0].name }}
        </div>
      </div>

      <div class="play">
        <van-icon
          :name="
            audioId !== item.id
              ? 'play-circle-o'
              : isPaused
              ? 'play-circle-o'
              : 'pause-circle-o'
          "
        />
      </div>

      <div class="share">
        <van-icon name="ellipsis" />
      </div>
    </div>

    <!-- 播放器 -->
    <Playbox></Playbox>
  </div>
</template>

<script>
import Playbox from "../components/Playbox.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Newsong",

  components: {
    Playbox,
  },

  data() {
    return {
      newsongData: [],
    };
  },

  created() {
    //获取新歌速递数据
    this.getNewsong();
  },

  computed: {
    ...mapState(["audioId", "audioEle", "isPaused", "songName", "songerName"]),
  },

  methods: {
    //返回
    onClickLeft() {
      this.$router.go(-1);
    },

    //获取新歌速递数据
    getNewsong() {
      this.axios({
        method: "get",
        url: "/top/song",
        params: {
          type: 0,
        },
      })
        .then((res) => {
          this.newsongData = res.data.data;
        })
        .catch((err) => {
          
        });
    },

    ...mapMutations(["changeStateProperty"]),

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
    },
  },
};
</script>

<style lang="less" scoped>
//导航栏
/deep/.van-nav-bar .van-icon {
  color: #3e3e3e;
  font-weight: 600;
}

/deep/.van-nav-bar__text {
  color: #3e3e3e;
  font-weight: 600;
}

//歌曲列表
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 0 14px;
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabtxt {
  display: flex;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.play {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 40px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: auto;
}
</style>